// pages/pkRecord/pkRecord.js
import {PksPaging} from "../../model/pks-paging"

Page({
  data: {
    list: [],
    loadingType: 'loading'
  },
  pksPaging: null,
  onLoad: async function (options) {
    const paging = await PksPaging.getLatestPaging()
    this.pksPaging = paging
    const data = await paging.getMoreData()
    console.log(data)
    if (!data) {
      return
    }
    data.accumulator.map(item => {
      item.create_time = this.formateDate(item.created_at)
    })
    this.setData({
      list: data.accumulator
    })
    if (!data.moreData) {
      this.setData({
        loadingType: 'end'
      })
    }
  },
  formateDate(datetime) {
    function addDateZero(num) {
      return (num < 10 ? "0" + num : num);
    }
    let d = new Date(datetime);
    let formatdatetime = d.getFullYear() + '-' + addDateZero(d.getMonth() + 1) + '-' + addDateZero(d.getDate()) + ' ' + addDateZero(d.getHours()) + ':' + addDateZero(d.getMinutes()) + ':' + addDateZero(d.getSeconds());
    return formatdatetime;
  },
  onReachBottom: async function () {
    const data = await this.pksPaging.getMoreData()
    console.log(data)
    if (!data) {
      return
    }
    data.accumulator.map(item => {
      item.create_time = item.created_at.substring(5, 10)
    })
    this.setData({
      list: data.accumulator
    })
    if (!data.moreData) {
      this.setData({
        loadingType: 'end'
      })
    }
  }
})
